!lm11
!rm75
Multiplying on the 6502

Brooke Boering wrote an excellent article, "Multiplying on the 6502", in MICRO--The 6502 Journal, December, 1980, pages 71-74.  If you are wondering how to do it, or you want a faster routine for a special application, look up that article.

Brooke begins by explaining and timing the multiply subroutine found in the old Apple Monitor ROM.  The time to multiply two 16-bit values and get a 32-bit result varies from 935 to 1511 microseconds, depending on how many "1" bits are in the multiplier.  He proceeds to modify that subroutine to cut the execution time by 40%!

Finally, he presents two limited versions which are still quite useful in some applications.  His 8x16 multiply averages only 383 microseconds, and his 8x8 version averages 192 microseconds.

Here is the code for his 16x16 version, which averages 726 microseconds.  It has the same setup as the routine in the Apple ROM.  On entry, the multiplicand should be in AUXL,AUXH ($54,55); the multiplier should be in ACL,ACH ($50,51); whatever is in XTNDL,XTNDH ($52,53) will be added to the product.  Normally, XTNDL and XTNDH should be cleared to zero before starting to multiply.  However, I have used this routine to convert from decimal to binary; I put the next digit in XTNDL and clear XTNDH, and then multiply the previous result by ten.  The "next digit" is automatically added to the product that way.  (I have corrected the typographical error in the listing as published in MICRO.)

    <<<code here>>>

I wrote a test routine for the multiply, so that I could check it out.  After assembling the whole program, I typed "MGO SETUP.Y" to link the control-Y Monitor Command to my test routine.  Control-Y will parse three 16-bit hexadecimal values this way:  val1<val2.val3cY stores val1 in $42,$43; val2 in $3C,$3D; and val3 in $3E,$3F.  ("cY" stands for control-Y.)

I define val1 to be the initial value for XTNDL,XTNDH; this should normally be zero.  The two values to be multiplied are val2 and val3.  After TESTMPY receives control from the control-Y processor, it moves the three values into the right locations for the multiply subroutine.  Then JSR RMUL calls the multiply routine.  The following lines (1570-1640) print the 32-bit result by calling a routine in the monitor ROM which prints a byte in hex from the A-register.
